<?php
include('./init.php');
include('./helper.php');

$files=scandir('./template');
isset($submit) or $submit='';
//string,callback,regexp
if($submit){
	$result=cp($text,$fields,$num,$sep);
	
}




?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>

<form method="post">
<table width="800" border="1">


  <tr>
    <th scope="row">字段</th>
    <td><textarea name="fields" cols="100" rows="10"><?php echo isset($fields)?$fields:'';?></textarea><br />每行一个，回车换行</td>
  </tr>
  <tr>
    <th scope="row">次数</th>
    <td><input type="text" name="num" id="" value="<?php echo isset($num)?$num:10;?>"></td>
  </tr>
  <tr>
    <th scope="row">分隔符</th>
    <td><input type="text" name="sep" id="" value="<?php echo isset($sep)?$sep:'_';?>"></td>
  </tr>
  <tr>
    <th scope="row">文本</th>
    <td>
    <select id="tpl">
    <option value="">====</option>
    <?php
    	foreach($files as $k=>$v){
			if($v=='.' || $v=='..') continue;
	?>
    	<option value="<?php echo './template/'.$v;?>"><?php echo $v;?></option>
	<?php
        }
    ?>
    </select>
    <textarea name="text" id="text" cols="100" rows="10"><?php echo isset($text)?$text:'';?></textarea><br />

	<input type="button" id="to_save" value="保存">
    </td>
  </tr>
  <tr>
    <th scope="row">结果</th>
    <td>
    <textarea name="result" id="result" cols="100" rows="10"><?php echo isset($result)?$result:'';?></textarea><br />
	
	</td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td align="center"><input type="submit" value="提交" name="submit" /></td>
  </tr>
</table>
</form>
<br />
<br />
<br />
<br />
<br />
<br />
<br />

<script src="jquery.js"></script>
<script>
$(document).ready(function(){
	$('#tpl').change(function(){
		if($(this).val()){
			$.post('get_content.php',{f:$(this).val()},function(data){
				$('#text').val(data);
			},'html');	
		}	
	});	
	$('#to_save').click(function(){
		if($('#text').val()){
			$.post('put_content.php',{f:$('#text').val()},function(data){
				$('#tpl').append("<option value='./template/"+data+"'>"+data+"</option>");
			},'html');	
		}	
	});
});
</script>
</body>
</html>
